package com.ja.openchat.viewmodels;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.user.UserInfo;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class MsgItemViewModel extends MultiItemViewModel<ChatSearchPageViewModel> {
    public ObservableField<MessageInfo> entity = new ObservableField<>();
    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> content = new ObservableField<>();
    public ObservableField<String> time = new ObservableField<>();
    public ObservableField<UserInfo> userAvatar = new ObservableField<>();

    public MsgItemViewModel(@NonNull ChatSearchPageViewModel viewModel, MessageInfo entity) {
        super(viewModel);
        this.entity.set(entity);
        update(entity);
    }

    private void update(MessageInfo messageInfo) {
//        helper.setGone(R.id.muteView,true);
//        helper.setGone(R.id.failView,true);
//        helper.setGone(R.id.indicatorView,true);
//        helper.setGone(R.id.tv_unread,true);

//        UserAvatarView avatarView = helper.getView(R.id.iv_icon);
//        avatarView.setTextSize(20);

        ChatInfo chat = TelegramManager.getTelegramManager().getTgData().chatInfo(messageInfo.chat_id);

//        if (chat.isGroup()) {//群组
//            //群组名称
//            helper.setText(R.id.iv_title, chat.title);
//            //群组头像
//            avatarView.showGroup(chat);
//        } else {//单聊
//            //对方昵称
//            helper.setText(R.id.iv_title, UserInfo.userDisplayName(chat.userId()));
//            //
//            UserInfo user = TelegramManager.getTelegramManager().getTgData().contactInfo(chat.userId());
//            if (user != null) {
//                avatarView.showUser(user);
//            } else {
//                avatarView.showName(UserInfo.userDisplayName(messageInfo.sender_user_id()));
//            }
//        }
//
//        TextView timeText = helper.getView(R.id.tv_time);
//        timeText.setText(DateHelper.getMessageFullDisplayTime(messageInfo.date * 1000, false));
//        timeText.setCompoundDrawables(null, null, null, null);
//        if (chat.isGroup()) {
//            UserInfo user = UserInfo.getUserInfo(messageInfo.sender_user_id());
//            if (user != null && !chat.lastMessage.isOutgoing() && !chat.lastMessage.isTipMessage()) {
//                helper.setText(R.id.tv_content, String.format("%s:%s", user.displayName(), messageInfo.toString()));
//            } else {
//                helper.setText(R.id.tv_content, messageInfo.toString());
//            }
//        } else {
//            helper.setText(R.id.tv_content, messageInfo.toString());
//        }
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
//    public int getPosition() {
//        return viewModel.getItemPosition(this);
//    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            Bundle bundle = new Bundle();
//            bundle.putLong("userId", entity.get().user_id);
//            viewModel.startContainerActivity(MyAlbumFragment.class.getCanonicalName(), bundle);
        }
    });
}
